package com.example.tally.frag_char;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import com.example.tally.R;
import com.example.tally.adapter.CharItemAdapter;
import com.example.tally.db.CharItemBean;
import com.example.tally.db.DBManager;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * 图形展示父类模块
 * update by mabb
 */
public abstract class BaseCharFragment extends Fragment {
    ListView charLv;
    int year;
    int month;
    List<CharItemBean> mData;
    private CharItemAdapter adapter;
    BarChart barChart;//代表柱状图的控件
    TextView textView; //没有收支情况怎么显示该控件

    Boolean isYear = false;  //是否选中年份

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_income_char, container, false);
        charLv = view.findViewById(R.id.frag_in_lv);
        // 获取Activity传递的数据
        Bundle bundle = getArguments();
        year = bundle.getInt("year");
        month = bundle.getInt("month");
        // 设置数据源
        mData = new ArrayList<>();
        // 设置适配器
        adapter = new CharItemAdapter(getContext(), mData);
        charLv.setAdapter(adapter);
        // 添加头布局
        addLvHeaderView();
        return view;
    }

    private void addLvHeaderView() {
        View view = getLayoutInflater().inflate(R.layout.item_charfrag_top, null);
        // 将view添加都头布局中
        charLv.addHeaderView(view);
        // 查找头布局当中包含的控件
        barChart = view.findViewById(R.id.item_charfrag_char);
        textView = view.findViewById(R.id.item_charfrag_top_tv);
        // 设置柱状图不显示描述
        barChart.getDescription().setEnabled(false);
        // 设置柱状图的内边距
        barChart.setExtraOffsets(20,20,20,20);
        // 设置坐标轴
        setAxis(year , month);
        // 设置坐标轴显示的数据
        setAxisData(year , month);
    }

    protected abstract void setAxisData(int year, int month);

    /**设置柱状图坐标轴的显示，方法必须重写，因为x轴相同，但是y轴不同*/
    protected void setAxis(int year, int month) {
        // 设置X轴
        XAxis xAxis = barChart.getXAxis();
        // 将X轴显示在下方
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        // 是否显示网格线
        xAxis.setDrawGridLines(true);
        // 设置X轴文字大小
        xAxis.setTextSize(12f);
        if(isYear) {
            // 设置X轴显示的值的格式
            // 设置X轴标签的个数
            xAxis.setLabelCount(12);
            xAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    int val = (int) value;
                    return String.valueOf(val+1);
                }
            });
        }else {
            // 设置X轴显示的值的格式
            // 设置X轴标签的个数
            xAxis.setLabelCount(31);
            xAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    int val = (int) value;
                    if(val == 0) {
                        return month + "-1";
                    }
                    if(val == 14){
                        return month + "-15";
                    }
                    if(month == 2){
                        if(val == 27){
                            return month + "-28";
                        }
                    }else if(month == 1 || month == 3 || month == 5 || month ==7
                            || month == 8 || month == 10 || month == 12){
                        if(val == 30){
                            return month + "-31";
                        }
                    }else if(month == 4 || month == 6 || month == 9 || month ==11){
                        if(val == 29){
                            return month + "-30";
                        }
                    }

                    return "";
                }
            });
        }

        // 设置标签对X轴的偏移量，垂直方向
        xAxis.setYOffset(10);

        // Y轴在子类的设置
        setYAxis(year , month);
    }
    /**设置Y轴，因为最高的坐标不确定，所以设置为抽象方法，在子类中实现*/
    protected abstract void setYAxis(int year , int month);

    public void setDate(int year , int month){
        this.year = year;
        this.month = month;
        barChart.clear();
        barChart.invalidate();  //重新绘制柱状图
        setAxis(year,month);
        setAxisData(year,month);
    }

    public void loaData(int year , int month , int kind) {
        List<CharItemBean> beanList = null;
        if(isYear) {
            beanList = DBManager.getCharListFromAccountYeartb(year, kind);
        }else {
            beanList = DBManager.getCharListFromAccounttb(year, month, kind);
        }
        mData.clear();
        mData.addAll(beanList);
        adapter.notifyDataSetChanged();
    }

    public Boolean getYear() {
        return isYear;
    }

    public void setYear(Boolean year) {
        isYear = year;
    }
}